<?php
namespace app\wx\controller;
use think\Request;
use think\Controller;
use think\Db;


class Index4 extends Controller
{

	/**
	 * 接收从小程序传来的文件
	 */
    public function index(){  
      //下面的file（）里的名字要和小程序里的name的值保持一致
    //  $this->logger($file);
    // $this->logger('通');
     //  exit;
        $file = request()->file('file');
         // 移动到框架应用根目录/public/uploads/ 目录下
         $this->logger($file);

		 if($file){
      $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
      if($info){
        // 成功上传后 获取上传信息
        // 输出 jpg
        $this->logger('成功');
        echo $info->getExtension();
        echo'<br/>';
        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
        echo $info->getSaveName()."<br/>";
        // 输出 42a79759f284b767dfcb2a0197904287.jpg
        echo $info->getFilename()."<br/>"; 
        $this->success('新增成功', 'Zshs/index48');
      }else{
        // 上传失败获取错误信息
       // echo $file->getError();
        $err = $file->getError();
        $this->logger('失败');

      }
    }


    }

   /*
*把接受到的消息存入 日志 ，如果日志大于100000 则删除一次日志
*/

private function logger($content){
  $logSize=100000;

  $log="../note/wx/log.txt";

  if(file_exists($log) && filesize($log)  > $logSize){
      unlink($log);
  }

  file_put_contents($log,date('H:i:s')." ".$content."\n",FILE_APPEND);

}

    /*临时
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/images');
    if($info){
        echo $info->getSaveName();
        die();
    }else{
        echo $file->getError();
        die();
    }
*/

/**
 * 小程序登陆
 */
  public function index2(){
    $code = input('get.code');
    // $this->logger($code);
    
    $appid = "wx3843c7001aba60e4";
    $secret = "8ba50984c338480a59566a6487e583f7";
    $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
	  $ch = curl_init();                      //向微信服务器请求
	  curl_setopt($ch, CURLOPT_URL,$api);
	  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
	  $data1 = curl_exec($ch);
    curl_close($ch);
    // $this->logger($data);
    
    $data = json_decode($data1,true);  
   if(isset($data['openid'])){
    $this->logger('获得的openid是：'.$data['openid']);
   //dump($data);
  // echo '获得的openid是:'.$data['openid'];
    }
   return $this->fetch();
   
}


/**
 * 小程序向服务器请求数据
 */
public function index3(){
// echo '检测';
$superior = input('get.superior');
// $superior = '获得'.$superior;
$table = 'note_list_xcx';  // 三段表名
$where = [
  'superior' =>  $superior,
];
$field = 'content,id,address';	
$res = $this->select1($table,$where,$field);

return json($res);
}





/**
 * 阿里云向小程序发图表需要的数据
 */
public function index4(){


/*
$nfs = "4000,5000,4500,5500";
$bfs ="500,550,400";
$nfs = explode(",",$nfs);
$bfs = explode(",",$bfs);
$data = array(
   "nfs"=>$nfs,
   "bfs"=>$bfs,
);
// dump($data); 
return json($data);
*/
$data = "4000,5000,4500,5500";
return json($data);
}





/**
 * 小程序 获得openid  接收来自《小程序笔记1109》 的26条 获得openid的访问
 */
public function index5(){
  $code = input('get.code');
  //$this->logger($code);
  $appid = "wx3843c7001aba60e4";  //对接专用小程序 不可更改
  $secret = "8ba50984c338480a59566a6487e583f7";
  $api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
  $ch = curl_init();                      //向微信服务器请求
  curl_setopt($ch, CURLOPT_URL,$api);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  $data1 = curl_exec($ch);
  curl_close($ch);
  // $this->logger($data);
  
  $data = json_decode($data1,true);  
 if(isset($data['openid'])){
  $this->logger('获得的openid是：'.$data['openid']);
 //dump($data);
  $data = '获得的openid是:'.$data['openid'];
  return json($data);
  }
}


/**
 * 小程序向服务器请求数据 返回一个字段的所有数据
 */
public function index6(){
// echo '检测';
$superior = input('get.superior');
// $superior = '获得'.$superior;
$table = 'note_list_xcx';  // 三段表名
$where = [
  'superior' =>  $superior,
];
$field = 'content';	
$res = $this->select2($table,$where,$field);

return json($res);

}

/**
 * 取出照片地址
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/index7
 */
public function index7(){
	$dir = opendir("/opt/lampp/htdocs/t5/public/static/note/img/test2");
        while (($file = readdir($dir)) !== false)
         {
		   //下面 if 开始	 
		   if($file != '.' && $file != '..'&& $file != 'test' ){
			
			$data = "http://www.hyhthjk.com.cn/static/note/img/test2/".$file;
		//	echo $data ."<br/>";
			$data1[] = $data;
		   }
		   //上面 if 结束
          }
		  closedir($dir);
		  return json($data1);

}

/**
 * search
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/index8?input=dsfkj
 */

public function index8(){
  $input = input('get.input');
  $table = 'note_list_xcx';  // 三段表名
  $where = [
    'content' =>  ['like','%'.$input.'%'],
  ];
  $field = 'content,id,address';	
  $res = $this->select1($table,$where,$field);
  return json($res);
}


/**
 * search
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/index9
 */
public function index9(){
  $data = [
    '0'=>[
         '0' => 'json',
         '1' => '工业出版社',
    ],
    '1'=>[
        '0'=>'python',
        '1'=>'农业出版社',
    ],
    '2'=>[
      '0'=>'css',
      '1'=>'华文出版社',
    ],
    ];
    $cyy = array(
      '0'=>array(
        '0'=>'仿京东登陆',
        '1'=>'家地址',
        '2'=>'note.php/boot/Login/index',
      ),
      '1'=>array(
        '0'=>'仿京东注册',
        '1'=>'地址3',
        '2'=>'link8',
    
      ),
      '2'=>array(
        '0'=>'横向二级菜单',
        '1'=>'家地址',
        '2'=>'note.php/boot/Caidan/index',
    
      ),
    );
  // [["仿京东登陆","家地址","note.php\/boot\/Login\/index"],["仿京东注册","地址3","link8"],["横向二级菜单","家地址","note.php\/boot\/Caidan\/index"]]
  return json($data);
}

/**
 * 为小程序note1220/map8提供数据库数据
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/index10
 */
public function index10(){
  // return json('https://www.hyhthjk.com.cn/note.php/wx/Index4/index10');
  // 临时链接 advertising数据库
  $M = $this->index11();
  // $sql = "select id,latitude,longitude,iconPath,biaoti as name from advertising_list";
  $data['latitude'] = 39.933730;
  $data['longitude'] = 116.396460;
  /*
  $min_longitude =  $data['longitude']-0.00478;
  $max_longitude =  $data['longitude']+0.00478;
  $min_latitude =  $data['latitude']-0.003530;
  $max_latitude =  $data['latitude']+0.003530;
  */
  $min_longitude =  73;
  $max_longitude =  135;
  $min_latitude =  18;
  $max_latitude =  50;
  $sql = "select id,latitude,longitude,iconPath,biaoti as name from advertising_list where longitude between $min_longitude and $max_longitude and latitude between $min_latitude and $max_latitude";
  // $res =	$M->query('select id,latitude,longitude,iconPath,biaoti as name from advertising_list' );
  $res =	$M->query($sql);
  // echo $M->getlastsql();
  
  $data['markers'] = $res;
  return json($data);
}

/**
 * 临时连接另一个数据库
 */
public function index11(){
// 	echo'连接数据库 要使用头部 use think\Db<br/>临时修改连接,改为查lya数据库';
		
	$res = Db::connect(
	[
	// 数据库类型
'type'            => 'mysql',
// 服务器地址
'hostname'        => '127.0.0.1',  // 阿里云为 localhost 在家 '127.0.0.1'
// 数据库名
'database'        => 'advertising',
// 用户名
'username'        => 'root',
// 密码
'password'        => '886400',          //在阿里云为886400  在家为''
// 端口
'hostport'        => '3306',
// 连接dsn
'dsn'             => '',
// 数据库连接参数
'params'          => [],
// 数据库编码默认采用utf8
'charset'         => 'utf8',
// 数据库表前缀
'prefix'          => 'advertising_',
	]
		);
//	dump($res);
   return $res;

}

/**
 * 根据 id 发送详细信息
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/index12?markerId=54
 */
 public function index12(){
  $markerId = input('get.markerId');
  // return json('服务器'.$markerId);
  $M = $this->index11();
  $sql = "select * from advertising_list where id = $markerId";
  $res =	$M->query($sql);
  return json($res);


 }






/*************************************************
 * 下面是model层
 */
public function select1($table,$where,$field){
  return Db::table($table)->where($where)->field($field)->select();	 
}

public function select2($table,$where,$field){
  return Db::table($table)->where($where)->column($field);	 
}



/**
 * 应 note1220 第86条 获得200条数据向前台渲染
 * https://www.hyhthjk.com.cn/note.php/wx/Index4/select4?id=300
 */
public function select4(){
  $id = input('get.id');
  $where = [
       'id'=>['lt',$id],
  ];
   $data = Db::table('note_listt')->where($where)->select();
    return json($data);
  // dump(Db::table('note_listt')->where($where)->select());
}



}